package com.magist.core.util;

import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MAssertUtil extends Assert {

    public static void longerThan(String text, int length, String message) {
        if (!StringUtils.hasLength(text) || !(text.length() > length)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void nullOrLongerThan(String text, int length, String message) {
        if (!StringUtils.hasLength(text)) {
            return;
        }
        if (!(text.length() > length)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void shorterThan(String text, int length, String message) {
        if (!StringUtils.hasLength(text) || !(text.length() < length)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void nullOrShorterThan(String text, int length, String message) {
        if (!StringUtils.hasLength(text)) {
            return;
        }
        if (!(text.length() < length)) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void isEmail(String email, String message) {
        Assert.notNull(email, message);
        String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void isNullOrEmail(String email, String message) {
        if(StringUtils.isEmpty(email)) {
            return;
        }
        String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(email);
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
    }


    public static void isMobile(String mobile, String message) {
        Assert.notNull(mobile, message);
        Pattern regex = Pattern.compile("^1[345789]\\d{9}$");
        Matcher matcher = regex.matcher(mobile);
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
    }


    public static void isNullOrMobile(String mobile, String message) {
        if(StringUtils.isEmpty(mobile)) {
            return;
        }
        Pattern regex = Pattern.compile("^1[345789]\\d{9}$");
        Matcher matcher = regex.matcher(mobile);
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void nullOrShorterThan(Double number, int length,String message) {
        if(ObjectUtils.isEmpty(number)) {
            return;
        }
        if(number<0) {
            throw new IllegalArgumentException("请输入正数");
        }
        StringBuilder max = new StringBuilder("0");
        for (int i = 0; i < length; i++) {
            max.append("9");
        }
        Double dMax = Double.valueOf(max.toString());
        if (number > dMax) {
            throw new IllegalArgumentException(message);
        }

    }

    public static void hasSize(Collection collection, String message) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 数字和字母
     * @param text
     * @return
     */
   public static void isNumOrLetter(String text, String message) {
        String regEx = "[a-zA-Z0-9]*";
        Pattern pattern = Pattern.compile(regEx);
        // 忽略大小写的写法
        // Pattern pat = Pattern.compile(regEx, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(text);
        // 字符串是否与正则表达式相匹配
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 字母和汉字
     * @param text
     * @return
     */
   public static void isLetterOrChinese(String text, String message) {
        String regEx = "[a-zA-Z\\u4e00-\\u9fa5]*";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(text);
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 数字和字母和汉字
     * @param text
     * @return
     */
   public static void isNumOrLetterOrChinese(String text, String message) {
        String regEx = "[a-zA-Z0-9\\u4e00-\\u9fa5]*";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(text);
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * 字母或汉字或数字或"_","-"
     * @param text
     * @return
     */
   public static void isNormal(String text, String message) {
        String regEx = "[a-zA-Z0-9\\u4e00-\\u9fa5_-]*";
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(text);
        boolean flag = matcher.matches();
        if (!flag) {
            throw new IllegalArgumentException(message);
        }
   }
}
